package Frame;

import VWorldGameStudio.FCMapAnimation;

import javax.swing.*;
import javax.swing.filechooser.FileSystemView;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.util.ArrayList;

public class FCEditMapAnimation extends JDialog {
    boolean isNew = true;
    private ArrayList<String> sucaiUrls = new ArrayList<>();
    private ArrayList<String> slectUrls = new ArrayList<>();

    public FCEditMapAnimation(boolean isNew, FCGMaster fcgMaster){
        setTitle((isNew) ? "创建地图动画" : "编辑地图动画");
        setBounds(0, 0, 500, 500);
        setLayout(null);
        setResizable(false);
        setLocationRelativeTo(null);
        setModal(true);

        JLabel b0 = new JLabel("动画名称:");
        b0.setBounds(10, 10, 60, 20);
        this.add(b0);
        JTextField b1 = new JTextField();
        b1.setBounds(80, 10, 150, 20);
        b1.setEditable(isNew);
        this.add(b1);

        JLabel b10 = new JLabel("选择编辑:");
        b10.setBounds(240, 10, 60, 20);
        this.add(b10);
        b10.setVisible(!isNew);
        JComboBox<String> b20 = new JComboBox<String>();
        b20.setBounds(310, 10, 120, 20);
        b20.addItem("null");
        for(FCMapAnimation animation: FCGMaster.mapAnimationLibrary.getAllAnimations()){
            b20.addItem(animation.getAName());
        }
        this.add(b20);
        b20.setVisible(!isNew);


        JLabel c2 = new JLabel("显示等级:");
        c2.setBounds(10, 30, 60, 20);
        this.add(c2);
        JComboBox<String> c20 = new JComboBox<String>();
        c20.setBounds(80, 30, 80, 20);
        c20.addItem("置于地面");//
        c20.addItem("置于人物");//
        this.add(c20);

        JLabel c0 = new JLabel("默认状态:");
        c0.setBounds(250, 30, 60, 20);
        this.add(c0);
        JComboBox<String> c1 = new JComboBox<String>();
        c1.setBounds(320, 30, 100, 20);
        c1.addItem("隐藏");//
        c1.addItem("显示");//
        this.add(c1);

        JLabel c3 = new JLabel("素材选择:");
        c3.setBounds(10, 60, 60, 20);
        this.add(c3);
        JComboBox<String> c30 = new JComboBox<String>();
        c30.setBounds(80, 60, 320, 20);
        this.add(c30);

        JButton selectUrl = new JButton("添加到动画");
        selectUrl.setBounds(240, 90, 130, 20);
        this.add(selectUrl);

        // 添加素材按钮
        JButton addButton = new JButton("添加素材");
        addButton.setBounds(380, 90, 110, 20);
        this.add(addButton);

        JLabel d3 = new JLabel("播放模式:");
        d3.setBounds(10, 100, 60, 20);
        this.add(d3);
        JComboBox<String> d3box = new JComboBox<String>();
        d3box.setBounds(80, 100, 80, 20);
        d3box.addItem("无限次");
        d3box.addItem("1次");
        d3box.addItem("2次");
        d3box.addItem("3次");
        d3box.addItem("4次");
        this.add(d3box);

        // 添加按钮监听器
        addButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                if (fileChooser.showOpenDialog(null) == fileChooser.APPROVE_OPTION) {
                    sucaiUrls.clear();
                    File file = new File(fileChooser.getSelectedFile().getParent());
                   // String path = fileChooser.getSelectedFile().getParent().replaceAll("\\\\", "/");
                    File[] files = file.listFiles();
                    if (files != null) {
                        for (File f : files) {
                            sucaiUrls.add(f.getAbsolutePath().replaceAll("\\\\", "/"));
                        }
                    }
                    c30.removeAllItems();
                    for(String s: sucaiUrls){
                        s = s.substring(s.lastIndexOf("/"));
                        c30.addItem(s);
                    }
                    c30.updateUI();
                }
            }
        });


        JPanel p = new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                g.setColor(Color.white);
                g.fillRect(0, 0, 200, 340);
                g.setColor(Color.black);
                for (int i = 0; i < slectUrls.size(); i++) {
                    String text = slectUrls.get(i);
                    text = text.substring(text.lastIndexOf("/"));
                    g.drawString(text, 5, i * 20 + 17);
                    g.drawLine(0, i * 20 + 20, 200, i * 20 + 20);
                    g.setColor(Color.white);
                    g.fillRect(180, i * 20 + 1, 20, 18);
                    g.setColor(Color.black);
                    g.drawLine(184, i * 20 + 10, 193, i * 20 + 10);
                }
            }
        };
        p.addMouseListener(new FCEditAnimation.Mlistener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getX() > 180) {
                    int a = e.getY() / 20;
                    if(a < slectUrls.size()){
                        slectUrls.remove(a);
                    }
                    p.updateUI();
                }
            }
        });
        p.setBounds(10, 130, 200, 340);
        p.setBorder(BorderFactory.createLineBorder(Color.black));
        this.add(p);

        ImageShow imageEdit = new ImageShow();
        imageEdit.setBounds(220, 130, 200, 200);
        this.add(imageEdit);

        selectUrl.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(c30.getItemCount() > 0) {
                    slectUrls.add(sucaiUrls.get(c30.getSelectedIndex()));
                    if(slectUrls.size() == 1){
                        imageEdit.setUrl(slectUrls.get(0));
                        imageEdit.updateUI();
                    }
                    p.updateUI();
                }
            }
        });

        // 确定按钮
        JButton okButton = new JButton("确定");
        okButton.setBounds(240, 420, 100, 20);
        this.add(okButton);
        okButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(b1.getText() != null && b1.getText().length() > 1 && slectUrls.size()>0){
                    int playTime = d3box.getSelectedIndex();
                    playTime = playTime==0? -1:playTime;
                    FCGMaster.mapAnimationLibrary.registerMapAni(b1.getText(), c20.getSelectedIndex(),
                            imageEdit.imgPngW,imageEdit.imgPngH, slectUrls, c1.getSelectedIndex(), playTime);
                    new FCTip((isNew) ? "新建动画" : "编辑动画", "成功!");
                    fcgMaster.reloadData();
                }else {
                    new FCTip((isNew) ? "新建动画" : "编辑动画", "放弃!");
                }
                dispose();
            }
        });

        b20.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                FCMapAnimation edit = FCGMaster.mapAnimationLibrary.getMapAnimationByName((String) b20.getSelectedItem());
                b1.setText(edit.getAName());
                b1.updateUI();
                c1.setSelectedIndex(edit.getVisiable());
                c20.setSelectedIndex(edit.getMapLevel());
                c1.updateUI();
                c20.updateUI();
                slectUrls.clear();
                slectUrls.addAll(edit.getAnimatImgUrls());
                p.updateUI();
                if(slectUrls.size() > 0) {
                    imageEdit.setUrl(slectUrls.get(0));
                    imageEdit.updateUI();
                }
                int playTime = edit.getNeedPlayTime() == -1? 0:edit.getNeedPlayTime();
                d3box.setSelectedIndex(playTime);
            }
        });
        setVisible(true);
    }


    // 文件选择
    private JFileChooser fileChooser = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory()) {
        protected javax.swing.JDialog createDialog(java.awt.Component parent) throws java.awt.HeadlessException {
            // 创建对话框
            JDialog fileChooserDialog = super.createDialog(parent);
            // 设置属性
            fileChooserDialog.setSize(500, 400);
            fileChooserDialog.setTitle("选择文件");
            fileChooserDialog.setResizable(false);
            fileChooserDialog.setLocationRelativeTo(null);
            return fileChooserDialog;
        }
    };

    class ImageShow extends JPanel {
        private String url;
        private Image animImg;

        public int imgPngW = 0,imgPngH = 0;

        public void setUrl(String u){
            url = u;
            animImg = Toolkit.getDefaultToolkit().getImage(url);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (animImg != null) {
                imgPngW = animImg.getWidth(this);
                imgPngH = animImg.getHeight(this);
                g.drawImage(animImg, 0, 0, imgPngW, imgPngH, this);
            }
        }
    }
}
